package com.esd.crm.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.esd.crm.entity.AreaCode;
import com.esd.crm.mapper.AreaCodeMapper;
import com.esd.crm.service.AreaCodeService;
import com.esd.crm.util.KitService;
import com.esd.vo.PageUtil;
import com.github.pagehelper.PageInfo;

@Service
public class AreaCodeServiceImpl implements AreaCodeService {
	
	private static Logger logger = Logger.getLogger(AreaCodeServiceImpl.class);
	
	@Autowired
	private AreaCodeMapper hcAreaCodeDao;
	
	@Override
	public boolean save(AreaCode object) {
		if (object == null) {
			logger.info("HcAreaCodeServiceImpl.save: 对象为空");
			return Boolean.FALSE;
		}else {
			if(StringUtils.isBlank(object.getId())) {
				object.setId(KitService.getUUID());
			}
			Date date = new Date();
			object.setCreateTime(date);
			object.setUpdateTime(date);
		}
		return hcAreaCodeDao.insertSelective(object);
	}

	@Override
	public boolean delete(String id) {
		return hcAreaCodeDao.deleteByPrimaryKey(id);
	}

	@Override
	public boolean deleteBatch(String[] ids) {
		return hcAreaCodeDao.deleteBatch(ids);
	}

	@Override
	public boolean update(AreaCode object) {
		Date date = new Date();
		if(object != null) {
			object.setUpdateTime(date);
		}
		return hcAreaCodeDao.updateByPrimaryKeySelective(object);
	}

	@Override
	public AreaCode findOne(String id) {
		return hcAreaCodeDao.selectByPrimaryKey(id);
	}

	@Override
	public AreaCode findOneAssociate(String id) {
		return null;
	}
	
	//根据地区代码获取其下面的地区列表
	@Override
	public List<AreaCode> getSubAreaAddress(String areaAddressKey) {
		String residenceAreaKeySql;
		// 1- 如果参数为空, 则获取省级列表
		if (StringUtils.isBlank(areaAddressKey)) {
			residenceAreaKeySql = "10__0000";
		} else if (areaAddressKey.endsWith("0000")) {
			// 2- 如果为省级, 则查询 下属的市级
			residenceAreaKeySql = "20" + areaAddressKey.substring(2, 4) + "__00";
		} else if (areaAddressKey.endsWith("00")) {
			// 3- 如果为市级 则查询下属的县区
			residenceAreaKeySql = "30" + areaAddressKey.substring(2, 6) + "__";
		} else {
			residenceAreaKeySql = areaAddressKey;
			areaAddressKey = null;
		}
		Map<String, String> areaAddressSqlMap = new HashMap<String, String>();
		areaAddressSqlMap.put("areaCodeSql", residenceAreaKeySql);
		areaAddressSqlMap.put("areaCode", areaAddressKey);
		return hcAreaCodeDao.retrieveSubAreaCode(areaAddressSqlMap);
	}
	
	@Override
	public String getAreaName(String areaCode) {
		String areaName = "";
		AreaCode hcAreaCode = hcAreaCodeDao.findOneByCode(areaCode);
		if(hcAreaCode != null) {
			areaName = hcAreaCode.getAreaName();
		}
		return areaName;
	}
		
	
	@Override
	public String getInfoByAreaCode(String areaCode) {
		String result = "";
		String province = "";
		String city = "";
		String district = "";
		if(StringUtils.isBlank(areaCode)) {
			result = "";
		}
		if(areaCode.startsWith("10")) {
			return hcAreaCodeDao.findOneByCode(areaCode).getAreaName();
		}else if(areaCode.startsWith("20")) {
			city = hcAreaCodeDao.findOneByCode(areaCode).getAreaCode();
			province = hcAreaCodeDao.findOneByCode("10" + areaCode.substring(2,4) + "0000").getAreaCode();
			result =  province + "-" + city;
		}else if(areaCode.startsWith("30")) {
			AreaCode districtArea = hcAreaCodeDao.findOneByCode(areaCode);
			if(districtArea == null) {
				district = "未知";
			}else {
				district = districtArea.getAreaName();
			}
			city = hcAreaCodeDao.findOneByCode("20" + areaCode.substring(2,6) + "00").getAreaName();
			province = hcAreaCodeDao.findOneByCode("10" + areaCode.substring(2,4) + "0000").getAreaName();
			result = province + "-" + city + "-" + district;
		}
		return result;
	}
	
	@Override
	public List<AreaCode> getAll(AreaCode t) {
		return hcAreaCodeDao.findAll(t);
	}

	@Override
	public Map<String, String> getMap() {
		List<AreaCode> all = hcAreaCodeDao.findAll(null);
		Map<String,String> map = new HashMap<String, String>();
		for (AreaCode hcAreaCode : all) {
			map.put(hcAreaCode.getAreaName(), hcAreaCode.getAreaCode());
		}
		return map;
	}

	@Override
	public PageInfo<AreaCode> findAllByPage(PageUtil page, AreaCode param) {
		return null;
	}
	
	@Override
	public void paraseAreaCode(String areaCode, Map<String, Object> entity) {
		//省级代码
		String provinceCode = "";
		//市级代码
		String cityCode = "";
		//区县级代码
		String districtCode = "";
		List<AreaCode> list = new ArrayList<AreaCode>();
		if(StringUtils.isBlank(areaCode)) {
			list = this.getSubAreaAddress("");
			entity.put("provinceList", list);
			return;
		}
		
		if(areaCode.startsWith("10")) {
			//1.证明是省级，查询省级列表以及市级列表
			provinceCode = areaCode;
			//省级列表
			list = this.getSubAreaAddress("");
			entity.put("provinceList", list);
			//市级列表
			list = this.getSubAreaAddress(areaCode);
			entity.put("cityList", list);
		}else if(areaCode.startsWith("20")) {
			//2.证明是市级，那么查询省市区列表
			//市级代码
			cityCode = areaCode;
			//省级代码
			provinceCode = "10" + areaCode.substring(2,4) + "0000";
			//区县级列表
			list = this.getSubAreaAddress(areaCode);
			entity.put("districtList", list);
			//省级列表
			list = this.getSubAreaAddress("");
			entity.put("provinceList", list);
			//市级列表
			list = this.getSubAreaAddress(provinceCode);
			entity.put("cityList", list);
		}else if(areaCode.startsWith("30")) {
			//3.证明是区县级、那么查询省市区列表
			districtCode = areaCode;
			provinceCode = "10" + areaCode.substring(2,4) + "0000";
			cityCode = "20" + areaCode.substring(2,6) + "00";
			//省级列表
			list = this.getSubAreaAddress("");
			entity.put("provinceList", list);
			//市级列表
			list = this.getSubAreaAddress(provinceCode);
			entity.put("cityList", list);
			//区县级列表
			list = this.getSubAreaAddress(cityCode);
			entity.put("districtList", list);
		}
		entity.put("provinceCode", provinceCode);
		entity.put("cityCode", cityCode);
		entity.put("districtCode", districtCode);
	}
	
	//根据省级代码、市级代码区级代码查询 地址信息
	@Override
	public Map<String,Object> paraseAreaCode(String provinceCode, 
			String cityCode, String districtCode, Map<String, Object> entity) {
		//省级代码 provinceCode
		//市级代码 cityCode
		//区县级代码 districtCode
		List<AreaCode> list = new ArrayList<AreaCode>();
		if(StringUtils.isBlank(provinceCode)) {
			//省级代码为空
			list = this.getSubAreaAddress("");
			entity.put("provinceList", list);
		}else {
			//省级代码不为空
			list = this.getSubAreaAddress("");
			entity.put("provinceList", list);
			//根据省级代码查出市级代码
			list = this.getSubAreaAddress(provinceCode);
			entity.put("cityList", list);
			if(!StringUtils.isBlank(cityCode)) {
				//如果市级代码不为空，则查询区县级信息
				list = this.getSubAreaAddress(cityCode);
				entity.put("districtList", list);
			}
			
		}
		return entity;
	}
}
